{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 04\n",
    "\n",
    "# 代数运算\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41c56724-a241-443c-9de3-77ca43ac2bb3",
   "metadata": {},
   "source": [
    "该代码使用`sympy`库创建并操作代数表达式，包括定义表达式、符号替换和表达式的值计算。首先，定义了一个三次多项式表达式：\n",
    "\n",
    "$$\n",
    "\\text{expr}_x = x^3 + 2x^2 - x - 2\n",
    "$$\n",
    "\n",
    "然后，将$x$替换为$1$来计算该表达式的具体值：\n",
    "\n",
    "$$\n",
    "\\text{expr}_x \\big|_{x=1} = 1^3 + 2 \\cdot 1^2 - 1 - 2 = 0\n",
    "$$\n",
    "\n",
    "接着，代码将表达式中的$x$替换为$\\cos(y)$，生成一个新的表达式$\\text{expr}_{\\cos y}$，表示三角函数的代数形式：\n",
    "\n",
    "$$\n",
    "\\text{expr}_{\\cos y} = \\cos(y)^3 + 2\\cos(y)^2 - \\cos(y) - 2\n",
    "$$\n",
    "\n",
    "在代码的最后部分，重新定义了符号$x$和$y$，并创建表达式$\\text{expr}_1 = x + y$，然后定义了新的符号$x_1$和$x_2$，创建表达式$\\text{expr}_2 = x_1 + x_2$。通过这种方式，代码展示了在符号表达式中如何定义、替换和重新定义符号以生成新表达式。\n",
    "\n",
    "这一过程体现了代数符号的灵活操作性，可以通过符号替换或重新定义来得到不同的表达式形式，从而实现对数学表达式的动态处理。这种符号替换和表达式生成的能力在公式推导、符号计算和解析函数构建中非常有用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2673cad2-1edd-48b9-801e-da59087e1a50",
   "metadata": {},
   "source": [
    "## 定义表达式并进行代入计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d913c630-ad07-4626-bd20-ed323772cc82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x^{3} + 2 x^{2} - x - 2$"
      ],
      "text/plain": [
       "x**3 + 2*x**2 - x - 2"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy.abc import x, y\n",
    "\n",
    "expr_x = x**3 + 2*x**2 - x - 2  # 定义表达式expr_x\n",
    "expr_x  # 输出表达式expr_x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cc6f5fa2-a796-4392-9836-85de0a87d7be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "print(expr_x.subs(x, 1))  # 将x替换为1后计算表达式值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "887d8a29-3ab0-40d3-824e-f891ad28b5f6",
   "metadata": {},
   "source": [
    "## 将表达式中的变量替换为cos(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "842e7862-85bf-4bba-86ba-d4b2b8356c8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import cos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9827c100-2aa3-4bb2-b495-7cedab84311d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\cos^{3}{\\left(y \\right)} + 2 \\cos^{2}{\\left(y \\right)} - \\cos{\\left(y \\right)} - 2$"
      ],
      "text/plain": [
       "cos(y)**3 + 2*cos(y)**2 - cos(y) - 2"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr_cos_y = expr_x.subs(x, cos(y))  # 将x替换为cos(y)后的新表达式expr_cos_y\n",
    "expr_cos_y  # 输出替换后的表达式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff458584-2d65-4dd9-8df9-632247eb12ff",
   "metadata": {},
   "source": [
    "## 定义新符号和表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2a62dfe7-1ee6-4845-9c27-5c8be7b33c2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import symbols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e4869ff7-f257-4218-8736-f199c762ea9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x + y$"
      ],
      "text/plain": [
       "x + y"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, y = symbols('x y')  # 定义符号x和y\n",
    "expr_1 = x + y  # 创建表达式expr_1\n",
    "expr_1  # 输出表达式expr_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "177c2c1f-eeb5-4ce2-a81f-6b6fccab480f",
   "metadata": {},
   "outputs": [],
   "source": [
    "x1, x2 = symbols('x1 x2')  # 定义符号x1和x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7c9af817-3743-4b24-b8e4-2c53e4cf678e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x_{1} + x_{2}$"
      ],
      "text/plain": [
       "x1 + x2"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr_2 = x1 + x2  # 创建表达式expr_2\n",
    "expr_2  # 输出表达式expr_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
